package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.AlumniOrganize;
import org.tea.inventory.db.domain.AlumniOrganizeUser;
import org.tea.inventory.db.domain.AlumniOrganizeUserCustom;
import org.tea.inventory.db.domain.AlumniUser;
import org.tea.inventory.db.service.AlumniOrganizeService;
import org.tea.inventory.db.service.AlumniOrganizeUserService;
import org.tea.inventory.db.service.UserService;
import org.tea.inventory.db.util.wx.entity.MessageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/organizeuser")
public class OrganizeUserController {

    @Autowired
    private AlumniOrganizeUserService alumniOrganizeUserService;

    @Autowired
    private AlumniOrganizeService alumniOrganizeService;

    @Autowired
    private UserService userService;

    /**
     * 根据orgId查询组织用户
     *
     * @param fkOrganizeId 组织id
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("selectOrganizeUserByOrgId")
    public Object selectOrganizeUserByOrgId(@LoginAdmin Integer adminId, Integer fkOrganizeId, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeUserService.selectByOrgId(fkOrganizeId,page,limit);

        return ResponseUtil.ok(map);
    }


    /**
     * 根据orgId查询组织正常用户
     *
     * @param fkOrganizeId 组织id
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("selectOrganizeUserByOrgIdNoDel")
    public Object selectOrganizeUserByOrgIdNoDel(@LoginAdmin Integer adminId, Integer fkOrganizeId, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeUserService.selectByOrgIdNoDel(fkOrganizeId,page,limit);

        List<AlumniOrganizeUserCustom> list = (List<AlumniOrganizeUserCustom>) map.get("items");

        for (AlumniOrganizeUserCustom alumniOrganizeUserCustom : list){
            if (alumniOrganizeUserCustom.getPriType().equals("1")){
                map.put("adminUser", alumniOrganizeUserCustom != null?alumniOrganizeUserCustom.getId():"");
                break;
            }
        }

        return ResponseUtil.ok(map);
    }

    @PostMapping("updateOrganizeUserById")
    public Object updateOrganizeUserById(@LoginAdmin Integer adminId,AlumniOrganizeUserCustom alumniOrganizeUserCustom){

        alumniOrganizeUserCustom.setModifyDate(LocalDateTime.now());
        alumniOrganizeUserCustom.setModifyUserId(adminId);

        // isDelete为false时添加删除时间
        if (alumniOrganizeUserCustom.getIsDelete() != null && !alumniOrganizeUserCustom.getIsDelete()){
            alumniOrganizeUserCustom.setDeleteDate(LocalDateTime.now());
        }

        Integer f = alumniOrganizeUserService.updateByPrimaryKeySelective(alumniOrganizeUserCustom);

        return ResponseUtil.ok(f);
    }

    @PostMapping("sendWXMsg")
    public Object sendWXMsg(Integer orgUserId){

        AlumniOrganizeUser alumniOrganizeUser = alumniOrganizeUserService.selectByPrimaryKey(orgUserId);

        AlumniOrganize alumniOrganize = alumniOrganizeService.selectByPrimaryKey(alumniOrganizeUser.getFkOrganizeId());

        // 发送微信消息
        AlumniUser u  = userService.findById(alumniOrganizeUser.getFkUserId());
        MessageData msgData = new MessageData();
        msgData.setKeyword1(u.getUserName());
        msgData.setKeyword2(alumniOrganize.getOrganizeName()+"专委会申请");
        if ("1".equals(alumniOrganizeUser.getIsNormal())){
            msgData.setKeyword3("通过");
            msgData.setKeyword4("您申请的"+ alumniOrganize.getOrganizeName()+"审核通过!");
        } else if ("2".equals(alumniOrganizeUser.getIsNormal())){
            msgData.setKeyword3("驳回");
            msgData.setKeyword4("您申请的"+ alumniOrganize.getOrganizeName()+"审核不通过!");
        }

        userService.sendWXMSG(alumniOrganizeUser.getFkUserId(),msgData);

        return ResponseUtil.ok();

    }
}
